Today, we’re making interactive plots in plotly. We’ll make examples using the Instacart data from the p8105.datasets package.
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(p8105.datasets)
library(dplyr)
library(flexdashboard)
knitr::opts_chunk$set(echo = TRUE)
library(plotly)
##
## Attaching package: 'plotly'
##
## The following object is masked from 'package:ggplot2':
##
## last_plot
##
## The following object is masked from 'package:stats':
##
## filter
##
## The following object is masked from 'package:graphics':
##
## layout
data("Instacart")
## Warning in data("Instacart"): data set 'Instacart' not found
janitor::clean_names(instacart)
I decided to keep three variables: order_hour_of_day,
department, days_since_prior_order.
instacart_ =
instacart |>
select(order_hour_of_day, department, days_since_prior_order)
Use plotly to make a histogram plot, a box plot, and a bar graph. First, we are making a histogram to see the distribution of orders by hour. The most orders were placed at 2pm.
instacart_ |>
plot_ly(
x = ~order_hour_of_day, type = "histogram",
marker = list(color = "pink",line = list(color = "grey",width = 2))) |>
layout(title = "Hours At Which Orders Were Placed",
xaxis = list(title = "Order Hours"),
yaxis = list(title = "Frequency"))
Next up - We are making a box plot. We are looking at department by the number of days since prior order.
instacart |>
plot_ly(
x = ~department, y = ~days_since_prior_order, color = ~department,
type = "box")